#pragma once
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "calendar.h"
using namespace std;


#pragma pack(1)
class CommonBCD
{
public:
	uint8_t v;
public:
	uint8_t get(void)const;
	void set(uint8_t s);
};
class MMWeekBCD
{
public:
	uint8_t mm:5;
	uint8_t ww:3;
public:
	uint8_t month(void)const;
	uint8_t week(void)const;
	void month(uint8_t m);
	void week(uint8_t w);
};
class CommonA01//ss:mm:hh DD-MM-WW-YY
{
public:
	CommonBCD ss;
	CommonBCD mm;
	CommonBCD hh;
	CommonBCD dd;
	MMWeekBCD mw;
	CommonBCD yy;
public:
	uint8_t second	(void)const;
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
	uint8_t month	(void)const;
	uint8_t week	(void)const;
	uint8_t year	(void)const;

	void second	(uint8_t v);
	void minute	(uint8_t v);
	void hour	(uint8_t v);
	void day	(uint8_t v);
	void month	(uint8_t v);
	void week	(uint8_t v);
	void year	(uint8_t v);
public:
	string toStringU(void)const;
	CommonA01& operator=(const Calendar& c);
	CommonA01(const Calendar& c=Calendar());
};
class CommonA07//XXX.X=3.1
{
public:
	CommonBCD aa;
	CommonBCD bb;
public:
	double get(void);
	void set(const double d);
public:
	CommonA07& operator=(const double d);
	CommonA07(const double d=0.0);
};
class CommonA08//XXXX=4
{
public:
	CommonBCD aa;
	CommonBCD bb;
public:
	double get(void);
	void set(const double d);
public:
	CommonA08& operator=(const double d);
	CommonA08(const double d=0.0);
};
class CommonA10//XXXXXX=6
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
public:
	double get(void);
	void set(const double d);
public:
	CommonA10& operator=(const double d);
	CommonA10(const double d=0.0);
};
class CommonA11//XXXX.XX=4.2
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
public:
	double get(void);
	void set(const double d);
public:
	CommonA11& operator=(const double d);
	CommonA11(const double d=0.0);
};
class CommonA12//XXXXXXXXXXXX=12
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
	CommonBCD dd;
	CommonBCD ee;
	CommonBCD ff;
public:
	double get(void);
	void set(const double d);
public:
	CommonA12& operator=(const double d);
	CommonA12(const double d=0.0);
};
class CommonA13//XXXX.XXXX=4.4
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
	CommonBCD dd;
public:
	double get(void);
	void set(const double d);
public:
	CommonA13& operator=(const double d);
	CommonA13(const double d=0.0);
};
class CommonA14//XXXXXX.XXXX=6.5
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
	CommonBCD dd;
	CommonBCD ee;
public:
	double get(void);
	void set(const double d);
public:
	CommonA14& operator=(const double d);
	CommonA14(const double d=0.0);
};
class CommonA15//{mm:hh DD-MM-YY}=(%y-%m-%d %H:%M)
{
public:
	CommonBCD mm;
	CommonBCD hh;
	CommonBCD DD;
	CommonBCD MM;
	CommonBCD YY;
public:
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
	uint8_t month	(void)const;
	uint8_t year	(void)const;
public:
	void minute	(uint8_t v);
	void hour	(uint8_t v);
	void day	(uint8_t v);
	void month	(uint8_t v);
	void year	(uint8_t v);
public:
	CommonA15& operator=(const string& s);
	CommonA15& operator=(const Calendar& c);
	CommonA15(const Calendar& c=Calendar());
public:
	string toStringU(void)const;
};
class CommonA16//{ss:mm:hh DD}=(%d %H:%M:%S)
{
public:
	CommonBCD ss;
	CommonBCD mm;
	CommonBCD hh;
	CommonBCD DD;
public:
	uint8_t second	(void)const;
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
public:
	void second (uint8_t v);
	void minute	(uint8_t v);
	void hour	(uint8_t v);
	void day	(uint8_t v);
public:
	CommonA15& operator=(const string& s);
	CommonA16& operator=(const Calendar& c);
	CommonA16(const Calendar& c=Calendar());
};
class CommonA17//{mm:hh DD-MM}=(%m-%d %H-%M)
{
public:
	CommonBCD mm;
	CommonBCD hh;
	CommonBCD DD;
	CommonBCD MM;
public:
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
	uint8_t month	(void)const;
public:
	void minute	(uint8_t v);
	void hour	(uint8_t v);
	void day	(uint8_t v);
	void month	(uint8_t v);
public:
	CommonA17& operator=(const string& s);
	CommonA17& operator=(const Calendar& c);
	CommonA17(const Calendar& c=Calendar());
};
class CommonA18//mm:hh DD
{
public:
	CommonBCD mm;
	CommonBCD hh;
	CommonBCD DD;
public:
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
public:
	void minute	(uint8_t v);
	void hour	(uint8_t v);
	void day	(uint8_t v);
public:
	CommonA18& operator=(const string& s);
	CommonA18& operator=(const Calendar& c);
	CommonA18(const Calendar& c=Calendar());
};
class CommonA19//mm:hh
{
public:
	CommonBCD mm;
	CommonBCD hh;
public:
	uint8_t minute	(void)const;
	uint8_t hour	(void)const;
public:
	void minute	(uint8_t v);
	void hour	(uint8_t v);
public:
	CommonA19& operator=(const string& s);
	CommonA19& operator=(const Calendar& c);
	CommonA19(const Calendar& c=Calendar());
};
class CommonA20//DD-MM-YY
{
public:
	CommonBCD DD;
	CommonBCD MM;
	CommonBCD YY;
public:
	uint8_t day		(void)const;
	uint8_t month	(void)const;
	uint8_t year	(void)const;
public:
	void day	(uint8_t v);
	void month	(uint8_t v);
	void year	(uint8_t v);
public:
	CommonA20& operator=(const string& s);
	CommonA20& operator=(const Calendar& c);
	CommonA20(const Calendar& c=Calendar());
public:
	string toStringU(void)const;
};
class CommonA21//{MM-YY}=(%y-%m)
{
public:
	CommonBCD MM;
	CommonBCD YY;
public:
	uint8_t month	(void)const;
	uint8_t year	(void)const;
public:
	void month	(uint8_t v);
	void year	(uint8_t v);
public:
	CommonA21& operator=(const string& s);
	CommonA21& operator=(const Calendar& c);
	CommonA21(const Calendar& c=Calendar());
};
class CommonA22//X.X=1.1
{
public:
	CommonBCD aa;
public:
	double get(void);
	void set(const double d);
public:
	CommonA22& operator=(const string& s);
	CommonA22& operator=(const double d);
	CommonA22(const double d=0.0);
};
class CommonA23//XX.XXXX=2.4
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
public:
	double get(void);
	void set(const double d);
public:
	CommonA23& operator=(const string& s);
	CommonA23& operator=(const double d);
	CommonA23(const double d=0.0);
};
class CommonA24//hh DD
{
public:
	CommonBCD hh;
	CommonBCD DD;
public:
	uint8_t hour	(void)const;
	uint8_t day		(void)const;
public:
	void hour	(uint8_t v);
	void day	(uint8_t v);
public:
	CommonA24& operator=(const string& s);
	CommonA24& operator=(const Calendar& c);
	CommonA24(const Calendar& c=Calendar());
};
class CommonA26//X.XXX=1.3
{
public:
	CommonBCD aa;
	CommonBCD bb;
public:
	double get(void);
	void set(const double d);
public:
	CommonA26& operator=(const string& s);
	CommonA26& operator=(const double d);
	CommonA26(const double d=0.0);
};
class CommonA27//XXXXXXXX=8
{
public:
	CommonBCD aa;
	CommonBCD bb;
	CommonBCD cc;
	CommonBCD dd;
public:
	double get(void);
	void set(const double d);
public:
	CommonA27& operator=(const string& s);
	CommonA27& operator=(const double d);
	CommonA27(const double d=0.0);
};
class Td_c : public CommonA15
{
public:
	uint8_t density;
	uint8_t number;
};
typedef CommonA20 Td_d;
typedef CommonA21 Td_m;

typedef	enum CommonC1
{
	DECITY_0_NONE  = 0,
	DECITY_1_15MIN = 1,
	DECITY_2_30MIN = 2,
	DECITY_3_60MIN = 3,
	DECITY_254_5MIN = 254,
	DECITY_255_1MIN = 255
}Dencity;
class DataType
{
public:
	uint8_t _DA1;
	uint8_t _DA2;
	uint8_t _DT1;
	uint8_t _DT2;
public:
	enum{
		PN_ZERO = 0,
		PN_MAX  = 2040,
		PN_ALL  = 0xFF00};
	enum{
		FN_MIN  = 1,
		FN_MAX  = 140};
public:
	void Pn(uint16_t n);
	void Fn(uint8_t n);
	uint16_t Pn(void)const;
	uint8_t  Fn(void)const;
	uint16_t make(uint8_t* buf);
public:
	operator int(void)const;
	DataType& operator=(uint32_t pnfn);
	DataType(uint8_t fn=0, uint16_t pn=0);
	bool append(class Buffer& b);
	string toStringU(void)const;
};
class Password
{
public:
	uint8_t pw[12];
public:
	Password& operator=(const string& s);
	bool append(class Buffer& b);
};
class ErrorCounter
{
public:
	uint8_t EC1;
	uint8_t EC2;
public:
	bool append(class Buffer& b);
};
class AUXTime
{
public:
	uint8_t		PFC;
	CommonA17	time;
	uint8_t		delay;
public:
	AUXTime(uint8_t pfc=0, uint8_t late=0)
		:PFC(pfc),delay(late){}
	bool append(class Buffer& b);
};
template <typename T>
string toString(T obj)
{
	string s;
	uint8_t *p = (uint8_t*)&obj;

	for(int i = 0; i < sizeof(obj); i++)
	{
		char buf[8];
		snprintf(buf, sizeof(buf), " %02X", p[i]);
		s += buf;
	}
	return s;
}
#pragma pack(0)
